﻿//扩展easyui表单的验证
$.extend($.fn.validatebox.defaults.rules, {
    //验证汉子
    CHS: {
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/.test(value);
        },
        message: '只能输入汉字'
    },
    //移动手机号码验证
    mobile: {//value值为文本框中的值
        validator: function (value) {
            var reg = /^1[3|4|5|8|9]\d{9}$/;
            return reg.test(value);
        },
        message: '输入手机号码格式不准确.'
    },
    //国内邮编验证
    zipcode: {
        validator: function (value) {
            var reg = /^[1-9]\d{5}$/;
            return reg.test(value);
        },
        message: '邮编必须是非0开始的6位数字.'
    },
    validateString: {
        validator: function (value, param) {
            var Max = param[0];
            var Min = param[1];
            var IsAllowNull = param[2];
            alert(Max);
            var widget = $(param[3]);
            if (value == '' && IsAllowNull == 0) {
                $.fn.validatebox.defaults.rules.validateString.message = 'Value cann\'t be empty';
                widget.linkbutton('disable');
                return false;
            } else if (value.length > Max || value.length < Min) {
                $.fn.validatebox.defaults.rules.validateString.message = 'Length of the value should be between ' + Min.toString() + ' ~ ' + Max.toString();
                widget.linkbutton('disable');
                return false;
            }
            widget.linkbutton('enable');
            return true;
        }, message: ''
    },
    fuelPrice: {
        validator: function (value, param) {
            var Max = param[0];
            var Min = param[1];
            var NumbersBeforeDecimalPoints = param[2];
            var NumbersAfterDecimalPoints = param[3];
            var IsAllowNull = param[4];
            var widget = $(param[5]);

            var bd, ad;
            if (value.indexOf('.') != -1) {
                var dn = value.split('.');
                bd = dn[0].length;
                ad = dn[1].length;
            }
            else {
                bd = value.length;
                ad = 0;
            }
            if (value == '' && IsAllowNull == 0) {
                $.fn.validatebox.defaults.rules.fuelPrice.message = 'Value cann\'t be empty';
                widget.linkbutton('disable');
                return false;
            } else if (isNaN(value)) {
                $.fn.validatebox.defaults.rules.fuelPrice.message = 'Please input a valid value';
                widget.linkbutton('disable');
                return false;
            } else if (value > Max || value < Min) {
                $.fn.validatebox.defaults.rules.fuelPrice.message = 'Value must be between ' + Min.toString() + ' ~ ' + Max.toString();
                widget.linkbutton('disable');
                return false;
            } else if (bd > NumbersBeforeDecimalPoints) {
                $.fn.validatebox.defaults.rules.fuelPrice.message = 'Allow only ' + NumbersBeforeDecimalPoints.toString() + ' numeric digits before the decimal point';
                widget.linkbutton('disable');
                return false;
            } else if (ad > NumbersAfterDecimalPoints) {
                $.fn.validatebox.defaults.rules.fuelPrice.message = 'Allow only ' + NumbersAfterDecimalPoints.toString() + ' numeric digits after the decimal point';
                widget.linkbutton('disable');
                return false;
            }
            widget.linkbutton('enable');
            return true;
        }, message: ''
    },
    //用户账号验证(只能包括 _ 数字 字母) 
    account: {//param的值为[]中值
        validator: function (value, param) {
            if (value.length < param[0] || value.length > param[1]) {
                $.fn.validatebox.defaults.rules.account.message = '用户名长度必须在' + param[0] + '至' + param[1] + '范围';
                return false;
            } else {
                if (!/^[\w]+$/.test(value)) {
                    $.fn.validatebox.defaults.rules.account.message = '用户名只能数字、字母、下划线组成.';
                    return false;
                } else {
                    return true;
                }
            }
        }, message: ''
    }
})